/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package wekimini.learning;

import java.util.logging.Level;
import java.util.logging.Logger;
import wekimini.util.Util;

/**
 *
 * @author rebecca
 */
public class KNNEditorPanel extends LearningModelBuilderEditorPanel {
    private static final Logger logger = Logger.getLogger(KNNEditorPanel.class.getName());
    /**
     * Creates new form AdaBoostEditorPanel
     */
    public KNNEditorPanel() {
        initComponents();
    }
    
    public KNNEditorPanel(KNNModelBuilder mb) {
        initComponents();
        textNumNeighbors.setText(Integer.toString(mb.getNumNeighbors()));
    }
    
    @Override
    public boolean validateForm() {
        try {
            int n = Integer.parseInt(textNumNeighbors.getText());
            if (n > 0) {
                return true;
            }
            Util.showPrettyErrorPane(this, "Number of neighbors must be an integer greater than 0");
            return false;
        } catch (NumberFormatException ex) {
            Util.showPrettyErrorPane(this, "Number of neighbors must be an integer greater than 0");
            return false;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textNumNeighbors = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        textNumNeighbors.setText("100");

        jLabel1.setText("Number of neighbors (k):");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textNumNeighbors, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(textNumNeighbors, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField textNumNeighbors;
    // End of variables declaration//GEN-END:variables

    //Assumes form is valid
    @Override
    public KNNModelBuilder buildFromPanel() {
        int numNeighbors = Integer.parseInt(textNumNeighbors.getText());
        logger.log(Level.INFO, "Creating new KNN model with {0} neighbors", numNeighbors);
        return new KNNModelBuilder(numNeighbors);
    }
}
